<?php
namespace app\baseinfo\model;

class Area extends \app\common\model\Base {
	protected $table='xl_area';
  use \app\common\model\TreeBase;
  protected $_AllowSiteInsulate=false;

  protected function _doing_updateDataById(&$param){
    if (db($this->table)->data([
      'id'=>$param['_id'],
      'pid'=>$param['pid'],
      'name'=>$param['name']
    ])->where('id', '=', $param['id'])->update()) {
      return true;
    } else {
      return "更新地区数据失败";
    }
  }

  protected function getFullNameAttr($val, $data) {
    if (!isset($data['id'])) {
      return 'null';
    }
    $ids = $this->getAllParent([$data['id']]);
    $ids[] = $data['id'];
    $names = $this->where('id', 'in', $ids)->column('name');
    if (count($names)) {
      return implode("/", $names);
    } else {
      return null;
    }
  }

  public static function byIP($ip) {
    $data = json_decode(file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip='.$ip));
    if ($data->data->city_id && $area=Area::get($data->data->city_id)) {
      return $area;
    } else {
      return null;
    }
  }

}
?>